package com.springboot.controller;

import com.springboot.dto.DoctorUnreadMessage;
import com.springboot.model.MessageInform;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorUnreadMessageNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * 本控制台用于医生查看未读消息数量
 */
@Controller
public class DoctorUnreadMessageNumberController {
    @Autowired
    DoctorUnreadMessageNumberService doctorUnreadMessageNumberService;
    /**
     * 医生查看自己的未读消息数量
     */
    @CrossOrigin
    @RequestMapping(value = "/getSelfInfo")
    public @ResponseBody
    String doctorUnreadMessageNumber(@RequestParam("username") String doctorTel) {
        System.out.println("医生查看自己的未读消息数量");
        try {
            UserLoginForm doctor=doctorUnreadMessageNumberService.findUserByTel(doctorTel);
            int doctorId=doctor.getUserId();
            ArrayList<MessageInform> messageInforms=doctorUnreadMessageNumberService.findMessageByDoctorId(doctorId);
            int unreadNumber=0;
            for(int i=0;i<messageInforms.size();i++)
            {
                MessageInform messageInform=messageInforms.get(i);
                if(messageInform.getMessageState2().equals("未读"))
                {
                    unreadNumber++;
                }
            }
            System.out.println("unreadNumber："+unreadNumber);
            DoctorUnreadMessage doctorUnreadMessage=new DoctorUnreadMessage();
            doctorUnreadMessage.setLogoLink(doctor.getLogoLink());
            doctorUnreadMessage.setUnreadNumber(unreadNumber);
            System.out.println(doctorUnreadMessage.toString());
            return doctorUnreadMessage.toString();
        } catch (Exception e) {
            return "False";
        }
    }
}
